package dhm.com.hpx.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.GetIndustryFollowBean;
import dhm.com.hpx.fragment.ShopHomeFragment;
import dhm.com.hpx.utils.SpUtils;
import dhm.com.hpx.utils.StringUtil;

public class IndustryActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.shai)
    TextView shai;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.todaa)
    LinearLayout todaa;
    @BindView(R.id.tab_homefg)
    TabLayout tabHomefg;
    @BindView(R.id.vp_homefg)
    ViewPager vpHomefg;
    private List<GetIndustryFollowBean.DataBean> tabEntities;
    private CategoryPageAdapter pageAdapter;
    private List<Fragment> fragments;

    private PressenterImpl pressenter;
    private String uid;
    @Override
    protected int getLayout() {
        return R.layout.activity_industry;
    }

    private String type="1";

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        type = getIntent().getStringExtra("type");
        uid = SpUtils.getString(this, "uid");
        fragments = new ArrayList<>();
        tabEntities = new ArrayList<>();

        pageAdapter = new CategoryPageAdapter(getSupportFragmentManager());
        vpHomefg.setAdapter(pageAdapter);
        tabHomefg.setupWithViewPager(vpHomefg);

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("type",type);
        map.put("user_id",uid);
        pressenter.sendMessage(this,Constant.list_industry_follow,map, GetIndustryFollowBean.class);
    }

    @Override
    protected void initData() {
//1文章，2视频，3海报
        switch (type){
            case "1":
                title.setText("获客文章");
                break;
            case "2":
                title.setText("获客视频");
                break;
            case "3":
                title.setText("行业海报");
                break;
            default:
        }
    }

    @OnClick({R.id.back, R.id.shai, R.id.search, R.id.add, R.id.todaa})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.todaa:
                intent=new Intent(IndustryActivity.this,FollowActivity.class);
                intent.putExtra("url","https://www.wenjuan.com/s/UZBZJvjnnE/");
                startActivity(intent);
                break;
            case R.id.shai:
                break;
            case R.id.add:
                intent=new Intent(this,EditActivity.class);
                intent.putExtra("industryType",type);
                startActivityForResult(intent,200);
                break;
            case R.id.search:
                intent=new Intent(this,SearchActivity.class);
                startActivity(intent);
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetIndustryFollowBean){
            GetIndustryFollowBean getIndustryFollowBean= (GetIndustryFollowBean) data;
            if (getIndustryFollowBean.getCode()==1){
                tabEntities.clear();
                fragments.clear();
                GetIndustryFollowBean.DataBean bean=new GetIndustryFollowBean.DataBean();
                bean.setIndustry_name("全部");
                bean.setIndustry_id(0);
                tabEntities.add(bean);
                tabEntities.addAll(getIndustryFollowBean.getData());
                for (GetIndustryFollowBean.DataBean dataBean : tabEntities) {
                    fragments.add(new ShopHomeFragment(dataBean.getIndustry_id(),type));
                }
                pageAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==200){
            Map<String,String> map=new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("type",type);
            map.put("user_id",uid);
            pressenter.sendMessage(this,Constant.list_industry_follow,map, GetIndustryFollowBean.class);
        }

    }

    //内部类写viewPage适配器
    class CategoryPageAdapter extends FragmentPagerAdapter {

        public CategoryPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return StringUtil.preventNull(tabEntities.get(position).getIndustry_name());
        }
    }

}
